package cn.invest.merry.module.invest.service.xjbase;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.invest.merry.module.invest.controller.admin.xjbase.vo.*;
import cn.invest.merry.module.invest.dal.dataobject.xjbase.XjBaseDO;
import cn.invest.merry.framework.common.pojo.PageResult;
import cn.invest.merry.framework.common.pojo.PageParam;
import cn.invest.merry.framework.common.util.object.BeanUtils;

import cn.invest.merry.module.invest.dal.mysql.xjbase.XjBaseMapper;

import static cn.invest.merry.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.invest.merry.module.invest.enums.ErrorCodeConstants.*;

/**
 * 个股报告期现金 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class XjBaseServiceImpl implements XjBaseService {

    @Resource
    private XjBaseMapper xjBaseMapper;

    @Override
    public Long createXjBase(XjBaseSaveReqVO createReqVO) {
        // 插入
        XjBaseDO xjBase = BeanUtils.toBean(createReqVO, XjBaseDO.class);
        xjBaseMapper.insert(xjBase);
        // 返回
        return xjBase.getId();
    }

    @Override
    public void updateXjBase(XjBaseSaveReqVO updateReqVO) {
        // 校验存在
        validateXjBaseExists(updateReqVO.getId());
        // 更新
        XjBaseDO updateObj = BeanUtils.toBean(updateReqVO, XjBaseDO.class);
        xjBaseMapper.updateById(updateObj);
    }

    @Override
    public void deleteXjBase(Long id) {
        // 校验存在
        validateXjBaseExists(id);
        // 删除
        xjBaseMapper.deleteById(id);
    }

    private void validateXjBaseExists(Long id) {
        if (xjBaseMapper.selectById(id) == null) {
            throw exception(XJ_BASE_NOT_EXISTS);
        }
    }

    @Override
    public XjBaseDO getXjBase(Long id) {
        return xjBaseMapper.selectById(id);
    }

    @Override
    public PageResult<XjBaseDO> getXjBasePage(XjBasePageReqVO pageReqVO) {
        return xjBaseMapper.selectPage(pageReqVO);
    }

}